package io.renren.modules.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.demo.dao.DhClubearningsInfoDao;
import io.renren.modules.demo.dao.DhGodsInfoDao;
import io.renren.modules.demo.dao.DhGodsrentInfoDao;
import io.renren.modules.demo.dao.DhGodssellInfoDao;
import io.renren.modules.demo.dto.ChildThreeData;
import io.renren.modules.demo.dto.DhGodsInfoDTO;
import io.renren.modules.demo.entity.DhClubearningsInfoEntity;
import io.renren.modules.demo.entity.DhGodsInfoEntity;
import io.renren.modules.demo.entity.DhGodsrentInfoEntity;
import io.renren.modules.demo.entity.DhGodssellInfoEntity;
import io.renren.modules.demo.service.DhGodsInfoService;
import io.renren.modules.demo.service.DhGodsrentInfoService;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-03-25
 */
@Service
public class DhGodsInfoServiceImpl extends CrudServiceImpl<DhGodsInfoDao, DhGodsInfoEntity, DhGodsInfoDTO> implements DhGodsInfoService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private DhGodsInfoDao dhGodsInfoDao;

    @Autowired
    private DhGodsrentInfoDao dhGodsrentInfoDao;

    @Autowired
    private DhGodssellInfoDao dhGodssellInfoDao;

    @Autowired
    private DhClubearningsInfoDao dhClubearningsInfoDao;

    @Override
    public QueryWrapper<DhGodsInfoEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<DhGodsInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    /**
     * 物品租赁
     *
     * @param id
     * @param userId
     * @param rent
     */
    @Override
    @Transactional
    public void rent(Integer id, Long userId, BigDecimal rent) {
        SysUserDTO sysUserDTO = sysUserService.getCLOUusername(userId);
        Integer username = Integer.valueOf(sysUserDTO.getUsername());
        DhGodsrentInfoEntity dhGodsrentInfoEntity = new DhGodsrentInfoEntity();
        int rentId = ((int) ((Math.random() * 9 + 1) * 10000)) + 200000;
        dhGodsrentInfoEntity.setId(rentId);
        dhGodsrentInfoEntity.setGodsId(id);
        dhGodsrentInfoEntity.setUserId(username);
        Date startTime = new Date();
        dhGodsrentInfoEntity.setStartTime(startTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.DAY_OF_YEAR, 7);
        Date endTime = calendar.getTime();
        dhGodsrentInfoEntity.setEndTime(endTime);
        dhGodsrentInfoEntity.setRent(rent);
        dhGodsrentInfoDao.insert(dhGodsrentInfoEntity);
        DhGodsInfoEntity dhGodsInfoEntity = dhGodsInfoDao.selectById(id);
        Integer amount = dhGodsInfoEntity.getAmount();
        dhGodsInfoEntity.setAmount(amount - 1);
        LambdaQueryWrapper<DhGodsInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(DhGodsInfoEntity::getId, id);
        dhGodsInfoDao.update(dhGodsInfoEntity, queryWrapper);
        DhClubearningsInfoEntity dhClubearningsInfoEntity = new DhClubearningsInfoEntity();
        dhClubearningsInfoEntity.setOrderId(rentId);
        dhClubearningsInfoEntity.setEarnings(rent);
        dhClubearningsInfoEntity.setCreateTime(startTime);
        dhClubearningsInfoDao.insert(dhClubearningsInfoEntity);
    }

    /**
     * 物品出售
     *
     * @param id
     * @param userId
     * @param sellPrice
     */
    @Override
    @Transactional
    public void sell(Integer id, Long userId, BigDecimal sellPrice) {
        SysUserDTO sysUserDTO = sysUserService.getCLOUusername(userId);
        Integer username = Integer.valueOf(sysUserDTO.getUsername());
        DhGodssellInfoEntity dhGodssellInfoEntity = new DhGodssellInfoEntity();
        int sellId = ((int) ((Math.random() * 9 + 1) * 10000)) + 300000;
        dhGodssellInfoEntity.setId(sellId);
        dhGodssellInfoEntity.setGodsId(id);
        dhGodssellInfoEntity.setUserId(username);
        dhGodssellInfoEntity.setSellPrice(sellPrice);
        dhGodssellInfoDao.insert(dhGodssellInfoEntity);
        DhGodsInfoEntity dhGodsInfoEntity = dhGodsInfoDao.selectById(id);
        Integer amount = dhGodsInfoEntity.getAmount();
        dhGodsInfoEntity.setAmount(amount - 1);
        LambdaQueryWrapper<DhGodsInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(DhGodsInfoEntity::getId, id);
        dhGodsInfoDao.update(dhGodsInfoEntity, queryWrapper);
        DhClubearningsInfoEntity dhClubearningsInfoEntity = new DhClubearningsInfoEntity();
        dhClubearningsInfoEntity.setOrderId(sellId);
        dhClubearningsInfoEntity.setEarnings(sellPrice);
        Date createTime = new Date();
        dhClubearningsInfoEntity.setCreateTime(createTime);
        dhClubearningsInfoDao.insert(dhClubearningsInfoEntity);
    }

    /**
     * 获取户外用品名称
     *
     * @param godsId
     * @return
     */
    @Override
    public String getGodsName(Integer godsId) {
        DhGodsInfoEntity dhGodsInfoEntity = dhGodsInfoDao.selectById(godsId);
        return dhGodsInfoEntity.getGodsName();
    }

    /**
     * 通过运动物品名称查找指定物品
     *
     * @param godsName
     * @return
     */
    @Override
    public List<DhGodsInfoDTO> getGodsDataListByGodsName(String godsName) {
        LambdaQueryWrapper<DhGodsInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(DhGodsInfoEntity::getGodsName, godsName);
        List<DhGodsInfoEntity> dhGodsInfoEntities = dhGodsInfoDao.selectList(queryWrapper);
        return ConvertUtils.sourceToTarget(dhGodsInfoEntities, DhGodsInfoDTO.class);
    }

    /**
     * 获取商品名称、库存数量
     *
     * @return
     */
    @Override
    public List<ChildThreeData> getChildThreeData() {
        return dhGodsInfoDao.getChildThreeData();
    }

    /**
     * 上架新的商品
     *
     * @param id
     * @param godsName
     * @param amount
     * @param rent
     * @param sellPrice
     * @return
     */
    @Override
    @Transactional
    public Integer newGods(Integer id, String godsName, Integer amount, BigDecimal rent, BigDecimal sellPrice) {
        DhGodsInfoEntity dhGodsInfoEntity = new DhGodsInfoEntity();
        dhGodsInfoEntity.setId(id);
        dhGodsInfoEntity.setGodsName(godsName);
        dhGodsInfoEntity.setAmount(amount);
        dhGodsInfoEntity.setRent(rent);
        dhGodsInfoEntity.setSellPrice(sellPrice);
        return dhGodsInfoDao.insert(dhGodsInfoEntity);
    }

    /**
     * 补充商品库存
     *
     * @param id
     * @param amount
     * @return
     */
    @Override
    @Transactional
    public Integer addGods(Integer id, Integer amount) {
        DhGodsInfoEntity dhGodsInfoEntity = dhGodsInfoDao.selectById(id);
        dhGodsInfoEntity.setAmount(dhGodsInfoEntity.getAmount() + amount);
        LambdaQueryWrapper<DhGodsInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(DhGodsInfoEntity::getId, id);
        return dhGodsInfoDao.update(dhGodsInfoEntity, queryWrapper);
    }
}